package org.szgb.console.sys.todo.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.szgb.console.sys.todo.entity.SysTodo;
import org.szgb.console.sys.todo.service.SysTodoService;
import org.szgb.console.utils.PageView;
import org.szgb.console.utils.TokenUtil;
import org.szgb.core.utils.AjaxJson;

@Slf4j
@RestController
@RequestMapping(value = "/sys/todo")
@Tag(name = "待办管理", description = "待办管理模块")
public class SysTodoController {
    private final SysTodoService sysTodoService;

    @Autowired
    public SysTodoController(SysTodoService sysTodoService) {
        this.sysTodoService = sysTodoService;
    }


    @Operation(summary = "获取待办信息", description = "根据查询参数获取待办信息。")
    @PostMapping("/get")
    public AjaxJson get(@RequestBody(required = false) SysTodo.SysTodoFindDto findDto, HttpServletRequest request, HttpServletResponse response) {
        if (findDto == null) {
            findDto = new SysTodo.SysTodoFindDto();
        }
        // 创建一个PageView对象，用于分页显示
        PageView<SysTodo.SysTodoView> pageView = new PageView<>();
        SysTodo sysTodo = findDto.toSysTodo();
        // 设置Page对象的列表属性，包含根据实体对象查询到的数据
        pageView.setList(SysTodo.SysTodoView.getViewListFromList(sysTodoService.findList(sysTodo)));
        // 设置Page对象的总记录数属性，用于分页显示
        pageView.setCount(sysTodoService.getCount(sysTodo));
        // 创建一个成功的AjaxJson对象，并放入Page对象，以便于前端展示
        return AjaxJson.success(request, response).put("page", pageView);

    }

    @PostMapping("/approve")
    @Operation(summary = "审批知会", description = "审批知会。")
    public AjaxJson approve(@RequestBody(required = false) SysTodo.SysTodoApprovalDto approvalDto, HttpServletRequest request, HttpServletResponse response) {
        sysTodoService.approvalInform(approvalDto.getId(), TokenUtil.getUid(request));
        return AjaxJson.success(request, response);
    }
}
